#!/usr/bin/env python

import sys
import os
import codecs

sys.path.append('/home/alittleluxury/pybarcode')
from barcode import get_barcode, get_barcode_class, __version__
try:
    from barcode.writer import ImageWriter
except ImportError:
    ImageWriter = None


code = 'code39'

bcodepath = '/home/alittleluxury/.wine/drive_c/merchant/barcodes'

bcodefiles = []
for file in os.listdir(bcodepath):
        if 'booth' in file and '.txt' in file:
                bcodefiles.append(os.path.join(bcodepath,file)
                
print 'Found the following barcode files:'
for fname in bcodefiles:
        print fname
		
if len(bcodefiles) > 1:
        print 'Found more than one barcode file. Please delete all barcode files except the one you want to print'
else:
        infile = open(bcodefiles[0],'r')
        allLines = infile.readlines()
        infile.close()
        incode = allLines[0].split('{')[1].split('}')[0]
        other = allLines[0].split('"')[1:3]
        booth,item,price = other[1],other[3],other[5]
        booth = int(booth)
        item = int(item)
        price = float(price)
        bcode = get_barcode(code,incode)
        text = 'booth %i, item: %i, $%.2f', (booth,item,price)
        
        if ImageWriter is not None:
            bcodec = get_barcode_class(code)
            bcode = bcodec(incode, writer=ImageWriter())
            opts = dict(font_size=14, text_distance=1)
            if code.startswith('i'):
                opts['center_text'] = False
            else:
                options['center_text'] = True
            filename = bcode.save(os.path.join(bcodepath, incode), opts)
            append_img(filename, bcode.name)
        else:
            objects.append(NO_PIL)
